//
//  BankListFillUpController.swift
//  d11
//
//  Created by rhonin on 2018/11/4.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
import SwiftyJSON
class BankListFillUpController: UpdateRootViewController {
      var timer = TimerHander()
    @IBOutlet weak var nameHeight: NSLayoutConstraint!
    var needName:String?{
        willSet{
            if (newValue != "true" && needTel != "true") {
                 pushToAddCard()
            }
        }
    }
    var needTel:String?{
        willSet{
            if (newValue != "true" && needName != "true") {
                pushToAddCard()
            }
        }
    }
    
    func pushToAddCard()  {
        
       
        self.navigationController?.popViewController(animated: true)
    }
    override func viewDidDisappear(_ animated: Bool) {
        if (needName != "true" && needTel != "true"){
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "checkBankSucess"), object: nil)
        }
    }
    @IBOutlet weak var viewTop: UIView!
    @IBOutlet weak var codeViewHieght: NSLayoutConstraint!
    
    @IBOutlet weak var viewBoton: UIView!
    @IBOutlet weak var viewMiddle: UIView!
    @IBOutlet weak var labcount: UILabel!
    @IBOutlet weak var nameText: UITextField!
    @IBOutlet weak var codeText: UITextField!
    @IBOutlet weak var telephoneText: UITextField!
    @IBOutlet weak var telephonViewHeight: NSLayoutConstraint!
    @IBOutlet weak var countButton: UIButton!
    @IBOutlet weak var sureBUtton: UIButton!
    
    @IBOutlet weak var codeHieght: NSLayoutConstraint!
    @IBOutlet weak var threelineHeight: NSLayoutConstraint!
    @IBOutlet weak var twolineHeight: NSLayoutConstraint!
    @IBOutlet weak var onelinHeight: NSLayoutConstraint!
    override func viewDidLoad() {
        super.viewDidLoad()
         self.viewBoton.layer.borderColor = RGBAlpa(219,219,219,1).cgColor
         self.viewMiddle.layer.borderColor = RGBAlpa(219,219,219,1).cgColor
         self.viewTop.layer.borderColor = RGBAlpa(219,219,219,1).cgColor
        let layerGradient = CAGradientLayer()
        layerGradient.colors = [
            UIColor.init(red: 1, green: 0.78, blue: 0.45, alpha: 1).cgColor,
            UIColor.init(red: 0.99, green: 0.57, blue: 0.33, alpha: 1).cgColor
        ]
        layerGradient.frame = CGRect(x: 0, y: 0, width: sureBUtton.frame.width, height: sureBUtton.frame.height+50)
        sureBUtton.layer.addSublayer(layerGradient)
        sureBUtton.layer.cornerRadius = 5
        self.title = "完善用户信息"
        if needName == nil{
            nameHeight.constant = 0
            onelinHeight.constant = 0
        }
        if needTel == nil {
            telephonViewHeight.constant = 0
            codeViewHieght.constant = 0
            twolineHeight.constant = 0
            threelineHeight.constant = 0
            codeHieght.constant = 0
        }
        self.countButton.clipsToBounds = true
        self.countButton.layer.cornerRadius = 6
        self.countButton.layer.borderColor = RGBAlpa(255,198,116,1).cgColor
        self.countButton.layer.borderWidth = 0.5
    }
    
    @IBAction func submit(_ sender: Any) {
        if needTel == "true"{
            updateUserTel()
        }else{
            loadViews = MBProgressHUD.showMessageNoHiden("正在提交.", to: self.view)
           updateUserName()
        }
    }
    
    @IBAction func getCode(_ sender: Any) {
       
        if telephoneText.text?.count ?? 0 > 0{
        startTimer()
        RequestHelper.post(atPath: "/clientsAPIs/GetMobileToken", withData: ["mobile":telephoneText.text!], withCompletion: { data in
            let json = JSON(data)
            let code = json["success"].boolValue
            if code  {
                DispatchQueue.main.async {
                    UIHelper.alertSimpleMessage(about: "获取短信验证码成功", withDescription: "请查看您的短信", in: self)
                    
                }
            }else{
                DispatchQueue.main.async {
                    
                    UIHelper.alertSimpleMessage(about: "获取短信验证码失败", withDescription: json["msg"].stringValue, in: self)
                    
                }
            }
        },withError: {_ in
             UIHelper.alertSimpleMessage(about: "服务器异常，\(getErrorString())", withDescription: "", in: self)
        })
        }else{
             UIHelper.alertSimpleMessage(about: "填写手机号码", withDescription: "", in: self)
        }
    }
    
    func  startTimer(){
        self.timer.startTimer(second: 60) { (count) in
            if count > 0{
                self.labcount.text =  "\(count)s"
                self.countButton.isUserInteractionEnabled = false
            }else{
                self.labcount.text = "获取验证码"
                self.countButton.isUserInteractionEnabled = true
            }
        }
    }
   var loadViews:MBProgressHUD?
    func updateUserTel(){
       
        if telephoneText.text?.count ?? 0 > 0{
            loadViews = MBProgressHUD.showMessageNoHiden("正在提交.", to: self.view)
            weak var this = self;
            RequestHelper.post(atPath: "/clientsAPIs/SaveMobile", withData: ["mobile":telephoneText.text!,"code":codeText.text!], withCompletion: { data in
                let json = JSON(data)
                let code = json["success"].boolValue
                if code {
                    DispatchQueue.main.async {
                        this!.needTel = "false"
                   //     UIHelper.alertSimpleMessage(about: "更改手机号成功", withDescription: "", in: self)
                    //    this?.perform(#selector(this?.popcont), with: nil, afterDelay: 0.5)
                    }
                }else{
                    DispatchQueue.main.async {
                        if(this!.needTel == "true"){
                            UIHelper.alertSimpleMessage(about: "更改失败", withDescription: json["msg"].stringValue, in: self)
                        }
                   //     UIHelper.alertSimpleMessage(about: "更改手机号失败", withDescription: json["msg"].stringValue, in: self)
                    //    this?.perform(#selector(this?.popcont), with: nil, afterDelay: 0.5)
                        
                    }
                }
                 DispatchQueue.main.async {
                    this!.updateUserName()
                }
            },withError: {_ in
                 UIHelper.alertSimpleMessage(about: "服务器异常，\(getErrorString())", withDescription: "", in: self)
            })
        }else{
            UIHelper.alertSimpleMessage(about: "填写手机号码", withDescription: "", in: self)
        }
        
    }
    
    func updateUserName(){
        weak var this = self;
        RequestHelper.post(atPath: "/kz/member/editrealname", withData: ["fullname":nameText.text!], withCompletion: { data in
            let json = JSON(data)
            let code = json["c"].intValue
            if code == 0{
                DispatchQueue.main.async {
                     this!.needName = "false"
//                    UIHelper.alertSimpleMessage(about: "更改姓名成功", withDescription: "", in: self)
//                    this?.perform(#selector(this?.popcont), with: nil, afterDelay: 0.5)
                      this?.loadViews?.removeFromSuperview()
                }
            }else{
                DispatchQueue.main.async {
                      this?.loadViews?.removeFromSuperview()
                    DispatchQueue.main.async {
                        if(this!.needName == "true"){
                            UIHelper.alertSimpleMessage(about: "更改失败", withDescription: json["msg"].stringValue, in: self)
                        }
                    }
//                    UIHelper.alertSimpleMessage(about: "更改失败", withDescription: json["msg"].stringValue, in: self)
//                    this?.perform(#selector(this?.popcont), with: nil, afterDelay: 0.5)
                    
                }
            }
        },withError: {_ in 
             UIHelper.alertSimpleMessage(about: "服务器异常，\(getErrorString())", withDescription: "", in: self)
        })
    }
    
    
}
